package net.hn.hnms.biz.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.ProjectSafety;
import net.hn.hnms.biz.project.mapper.ProjectSafetyMapper;
import net.hn.hnms.biz.project.request.ProjectSafetyReq;
import net.hn.hnms.biz.project.service.ProjectSafetyService;
import net.hn.hnms.biz.project.vo.ProjectSafetyVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectSafetyServiceImpl implements ProjectSafetyService {


    @Autowired
    private ProjectSafetyMapper projectSafetyMapper;

    @Override
    public ResponsePage<ProjectSafetyVo> selectPageList(ProjectSafetyReq accidentInfoReq, PageQuery pageQuery) {
        Page<ProjectSafety> page = projectSafetyMapper.selectPage(pageQuery.build(), buildQuery(accidentInfoReq).orderByDesc(ProjectSafety::getCreateTime));
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    @Override
    public int insert(ProjectSafetyReq accidentInfoReq) {
        ProjectSafety insertObj = new ProjectSafety();
        BeanUtil.copyProperties(accidentInfoReq, insertObj);
        return projectSafetyMapper.insert(insertObj);
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        projectSafetyMapper.updateDeleteByIds(idList);
    }

    @Override
    public void updateById(ProjectSafetyReq contractInfoReq) {
        ProjectSafety updateObj = new ProjectSafety();
        BeanUtil.copyProperties(contractInfoReq, updateObj);
        int updateCount = projectSafetyMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public ProjectSafety selectDetailById(String id) {
        ProjectSafety contractInfo = projectSafetyMapper.selectById(id);
        if (contractInfo == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return contractInfo;
    }



    private LambdaQueryWrapper<ProjectSafety> buildQuery(ProjectSafetyReq req){
        return new LambdaQueryWrapper<ProjectSafety>()
                .like(StrUtil.isNotBlank(req.getUnitName()), ProjectSafety::getUnitName, req.getUnitName())
                .eq(req.getCheckDate() != null, ProjectSafety::getCheckDate, req.getCheckDate())
                .eq(StrUtil.isNotBlank(req.getCheckResult()), ProjectSafety::getCheckResult, req.getCheckResult())
                .eq(StrUtil.isNotBlank(req.getMineCode()), ProjectSafety::getMineCode, req.getMineCode());
    }
}
